package middleware

import (
	"gfDemo/internal/service"
	"github.com/gogf/gf/v2/net/ghttp"
	"net/http"
)

type sMiddleware struct {
}

func init() {
	service.RegisterMiddleware(&sMiddleware{})
}

func (s *sMiddleware) Ctx(r *ghttp.Request) {
	service.BizCtx().Init(r)
	r.Middleware.Next()
}

func (s *sMiddleware) Auth(r *ghttp.Request) {
	if service.User().IsSignedIn(r.Context()) {
		r.Middleware.Next()
	} else {
		r.Response.WriteStatus(http.StatusForbidden)
	}
}

// CORS allows Cross-origin resource sharing.
func (s *sMiddleware) CORS(r *ghttp.Request) {
	r.Response.CORSDefault()
	r.Middleware.Next()
}
